#include "promsmodel.h"

PromsModel::PromsModel( QObject *parent) :
    QSqlQueryModel(parent)
{
}

void PromsModel::setFams(QSqlDatabase db, int fams, bool actif, const QStringList &l)
{
    QString r = "SELECT proms FROM v_users WHERE fams = :f";

    QSqlQuery q(db);

    if(actif){
        r += " AND actif = 1";
    }

    if(!l.isEmpty())
    {
        r+= " AND proms IN ('" + l.join("','") + "')";
    }
    q.prepare(r);
    q.bindValue(":f",fams);
    q.exec();
    this->setQuery(q);

}

PromsModel *PromsModel::allProms(QSqlDatabase db, QObject *parent)
{
    PromsModel *model =  new PromsModel(parent);
    if(db.driverName() == "QMYSQL"){
        model->setQuery("SELECT CONCAT(centre,' ',annee,statut) FROM promotions",db);
    } else {
        model->setQuery("SELECT proms FROM promotions",db);
    }
    model->setHeaderData(0,Qt::Horizontal,"Prom's");
    return model;
}


QVariant PromsModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(orientation == Qt::Vertical && role == Qt::DisplayRole){
        return QVariant("F"+QString::number(section+1)+" -> ");
    }

    return QSqlQueryModel::headerData(section,orientation,role);
}
